package sg.edu.nus.iss.ooad.jass.ui;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import sg.edu.nus.iss.ooad.jass.controller.AdvertiserController;
import sg.edu.nus.iss.ooad.jass.entity.Advertiser;
import sg.edu.nus.iss.ooad.jass.ui.widget.AbstractJASSRegularWindow;
import sg.edu.nus.iss.ooad.jass.ui.widget.JASSRegularPanel;

/**
 *
 * Create Job Advertisement Form.
 *
 * @author Isak Rabin (HT082193M)
 * @version 1.0
 * @since 1.0
 *
 * Development Environment : Netbeans IDE v6.7
 * Name of the File        : AdvertiserCreateJobAdvertisementForm.java
 * Creation / Modification History
 *         Isak Rabin        18-Oct-2009        Created
 *
 */
public class JASSCreateAdvertiserForm extends AbstractJASSRegularWindow {

        private static final long serialVersionUID = 5373656770749921958L;
        private final static Logger logger = Logger.getLogger(JASSUpdateAdvertiserForm.class.getName());
        
        //Variables declarations
        //Form Components
        private Button btnCancel = null;
        private Button btnSave = null;
        private Label labelError = null;
        private Label userID = null;
        private Label userName = null;
        private Label password = null;
        private TextField textUserID = null;
        private TextField textName = null;
        private TextField textPassword = null;
        private JASSRegularPanel formPanel = null;
        private Panel inputPanel = null;
        private Panel buttonPanel = null;
        private GridBagLayout inputLayout = null;
        // End of variables declaration
        private AbstractJASSRegularWindow parent;


        /**
        * Contructor of JASSCreateAdvertiserForm class
        * @param parent
        */
        public JASSCreateAdvertiserForm(AbstractJASSRegularWindow parent) {
                super("Job Advertisement and Search System", new Dimension(550, 400));
                initComponents();
                this.parent = parent;

        }

      /**
       * init the component
       */
        private void initComponents() {

                labelError = new Label();
                userID = new Label("User ID");
                textUserID = new TextField(25);
                userName = new Label("Name");
                textName = new TextField(35);
                password = new Label("Password");
                textPassword = new TextField(15);
                textPassword.setEchoChar('*');
                btnSave = new Button("Save");
                btnCancel = new Button("Cancel");


                //set panel
                buttonPanel = new Panel();
                inputPanel = new Panel();
                buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
                inputLayout = new GridBagLayout();
                inputPanel.setLayout(inputLayout);
                buttonPanel.add(btnSave);
                buttonPanel.add(btnCancel);

                //set actionlistener
                btnSave.addActionListener(new CreateActionListener());
                btnCancel.addActionListener(new CancelActionListener());

                attachComponent(labelError, 0, 0, 2, 1);
                attachComponent(userID, 0, 1, 1, 1);
                attachComponent(textUserID, 1, 1, 1, 1);
                attachComponent(userName, 0, 2, 1, 1);
                attachComponent(textName, 1, 2, 1, 1);
                attachComponent(password, 0, 3, 1, 1);
                attachComponent(textPassword, 1, 3, 1, 1);

                formPanel = new JASSRegularPanel("Create Advertiser");
                formPanel.add(inputPanel, BorderLayout.CENTER);
                formPanel.add(buttonPanel, BorderLayout.SOUTH);

                //add to the frame
                add(formPanel);
                pack();
        }
/**
 * Attach the component to the form.
 * @param c
 * @param gridx
 * @param gridy
 * @param gridwidth
 * @param gridheight
 * @param fill
 */
        @Override
        public void attachComponent(Component c, int gridx, int gridy, int gridwidth, int gridheight, int fill) {
                GridBagConstraints constraint = new GridBagConstraints();
                constraint.gridx = gridx;
                constraint.gridy = gridy;
                constraint.gridwidth = gridwidth;
                constraint.gridheight = gridheight;
                constraint.fill = fill;
                constraint.insets = new Insets(4, 4, 4, 4);
                constraint.weightx = 1.0;
                constraint.weighty = 1.0;
                inputPanel.add(c);
                inputLayout.setConstraints(c, constraint);
        }
        /**
         * Reset the form
         */
        @Override
        public void resetForm() {
                throw new UnsupportedOperationException("Not supported yet.");
        }

       /**
        * Handle the  action to create advertiser
        */
        class CreateActionListener implements ActionListener {

                public void actionPerformed(ActionEvent e) {
                        AdvertiserController advertController = new AdvertiserController();

                        String userId = textUserID.getText();
                        String userName = textName.getText();
                        String password = textPassword.getText();

                        if (userId == null || "".equalsIgnoreCase(userId)) {
                                labelError.setText("Please provide User ID");
                                return;
                        }

                        if (userName == null || "".equalsIgnoreCase(userName)) {
                                labelError.setText("Please provide User Name");
                                return;
                        }

                        if (password == null || "".equalsIgnoreCase(password)) {
                                labelError.setText("Please provide Login Password");
                                return;
                        }

                        boolean success = false;
                        try {
                                Advertiser advertiser = new Advertiser("", textUserID.getText(), textName.getText(), textPassword.getText(), "0");
                                success = advertController.createAdvertiser(advertiser);
                                if ( success) {
                                     displayMessage ("Advertiser Info", "Created successfully");
                                     parent.resetForm();
                                     close();
                                }else {

                                    displayMessage ("Advertiser Info", "Failed to create");
                                 }
                        } catch (Exception ex) {
                                logger.log(Level.SEVERE, null, ex);
                        }

                }
        }

        /**
         * Close Action Handler. It is called when the user click button "Cancel"
         */
        class CancelActionListener implements ActionListener {
                public void actionPerformed(ActionEvent e) {
                        close();
                }
        } 

}
